package com.smile.frame.mq.rocket.customer;

import com.smile.frame.mq.rocket.constants.cart.CartTopic;
import com.smile.frame.mq.rocket.message.CartItemEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/***
 * 购物车事件 消费者
 * @author LGC
 */
@Slf4j
@Service
public class CartEventConsumer {

    @Service
    @RocketMQMessageListener(
            consumerGroup = "${rocketmq.system.cartConsumerGroup}",
            topic = "${rocketmq.system.cartAddTopic}"
    )
    public static class CardItemAddConsumer implements RocketMQListener<CartItemEvent> {
        @Override
        public void onMessage(CartItemEvent addItemEvent) {
            log.info("topic:{},event: {}", CartTopic.CART_ADD_TOPIC, addItemEvent);
            // additional logic
//            throw new RuntimeException("222222222222");
        }
    }


    /**
     * 顺序消费模式
     */
    @Service
    @RocketMQMessageListener(
            consumerGroup = "${rocketmq.system.cartConsumerGroup}",
            topic = "${rocketmq.system.cartAddOrderlyTopic}",
            consumeMode = ConsumeMode.ORDERLY
    )
    public static class CardItemAddOrderlyConsumer implements RocketMQListener<CartItemEvent> {
        @Override
        public void onMessage(CartItemEvent addItemEvent) {
            log.info("topic:{},event: {}", CartTopic.CART_ADD_ORDERLY_TOPIC, addItemEvent);
            // additional logic
        }
    }


    @Service
    @RocketMQMessageListener(
            consumerGroup = "${rocketmq.system.cartConsumerGroup}",
            topic = "${rocketmq.system.cartRemovedTopic}"
    )
    public static class CardItemRemoveConsumer implements RocketMQListener<CartItemEvent> {
        @Override
        public void onMessage(CartItemEvent removeItemEvent) {
            log.info("topic:{},event: {}", CartTopic.CART_REMOVED_TOPIC, removeItemEvent);
            // additional logic
        }
    }

}